var app = getApp();
var api = require('../../api.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    password:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var id = options.id;
    if (options.scene) {
      id = decodeURIComponent(options.scene);
    }
    var page = this;
    page.setData({
      video_id: id,
      store: wx.getStorageSync('store')
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var page = this;
    var video_id = page.data.video_id;
    app.request({
      url: api.user.video,
      data: { video_id: video_id },
      success: function (res) {
        if (res.code == 0) {
          var video = res.data.video;
  
          if (video.status == 1) {
            wx.showModal({
              title: '提示',
              content: '该内容已下架',
              showCancel: false,
              confirmText: '去首页',
              success: function (res) {
                if (res.confirm) {
                  wx.switchTab({
                    url: '/pages/index/index',
                  })
                }
              }
            })
          } else {
            page.setData({
              video: res.data.video,
              video_list: res.data.video_list,
              video_id: res.data.video.id,
            });
            // wx.setNavigationBarTitle({
            //   title: res.data.video.title,
            // })
          }
        }
      },
      complete: function (res) {
       
      }
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },
  formSubmit: function (e) {
    var page = this;
    var password = e.detail.value.password;
    if (password==''){
      wx.showToast({
        title: '请输入密码',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    var video = page.data.video;
    var video_id = page.data.video_id;
    if (password == video.password){
      wx.showToast({
        title: '密码成功',
        icon: 'success'
      });
      setTimeout(function () {
        wx.redirectTo({
          url: '/pages/video-live/video-live?id=' + video_id,
        })
      }, 2000)
    }else{
      wx.showToast({
        title: '密码错误',
        image: '/images/icon-warning.png',
        duration: 2000
      })
      }
  },
})